package com.eric.husky.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import com.eric.husky.common.CommonController;
import com.eric.husky.common.web.ResponseModel;
import com.eric.husky.bean.Task;
import com.eric.husky.service.TaskService;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "/task")
@RestController
@Api(value="任务管理接口",tags={"Task(任务管理)-增删改查;导入导出"})
public class TaskController extends CommonController{

	@Autowired
	private TaskService taskService;

	@ApiOperation(value = "获取分页数据" ,notes = "获取分页数据" )
    @GetMapping("/list")
    public Page<Task> list(Task task, Pageable page) {
        return taskService.list(task, page);
    }

    @ApiOperation(value = "获取自定义分页数据" ,notes = "获取自定义分页数据" )
    @GetMapping("/taskpage")
    public Page<Map<String , Object>> getTaskPage(Pageable page, String keyword) {
        return taskService.getTaskPage(page,keyword);
    }

    @ApiOperation(value = "获取列表数据" ,notes = "获取列表数据" )
	@GetMapping("/findAll")
	public List<Task> findAll(){
		return taskService.findList();
	}

    @ApiOperation(value = "获取单条数据对象" ,notes = "获取单条数据对象")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query",name = "taskid" ,value = "taskID" , required = true, dataType = "Integer")
	})
	@GetMapping("/{taskid}")
    public Task get(@PathVariable Integer taskid) {
		return taskService.get(taskid);
    }

    @ApiOperation(value = "保存单条数据", notes = "保存单条数据,id列为空则为新增,不为空则为修改")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Task",value = "task",required = false,dataType = "Task")
    })
    @PostMapping("/save")
    public Task save(@RequestBody Task task) {
		return taskService.save(task);
    }

    @ApiOperation(value = "删除任务管理", notes = "删除任务管理" )
    @ApiImplicitParams({ @ApiImplicitParam(name = "taskids", value = "TaskID", required = true, dataType = "String")
    })
    @RequestMapping(value = "/delete" ,method = { RequestMethod.DELETE})
    public ResponseModel delete(String taskids) {
        try {
            String[] id_array = taskids.split(",");
            for(String taskid:id_array){
                taskService.delete(Integer.parseInt(taskid));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseModel.success("删除成功");
    }

}